package com.eportal.struts.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import cfca.payment.api.system.PaymentEnvironment;

import com.eportal.ORM.CsSysTerminal;
import com.eportal.ORM.CsSysTerminalBak;
import com.eportal.ORM.CsSysTerminalHb;
import com.eportal.ORM.MklInfo;
import com.eportal.ORM.MpayGroup;
import com.eportal.ORM.MpayModule;
import com.eportal.ORM.MpayOrg;
import com.eportal.ORM.MpayShop;
import com.eportal.ORM.MpayTradeTable;
import com.eportal.ORM.MpayUser;
import com.eportal.ORM.TermReadyView;
import com.eportal.ORM.UserView;
import com.eportal.core.BaseAction;
import com.eportal.core.Menu;
import com.eportal.core.Page;
import com.eportal.core.QQWry;
import com.eportal.core.Util;
import com.eportal.gd.Conf;
import com.eportal.service.AdminService;

@SuppressWarnings("serial")
public class AdminAction extends BaseAction {


	public String password() {
		LOG.info("修改密码");
		UserView m = (UserView) getRequest().getSession().getAttribute(
				"mpay_user");
		MpayUser mpay = (MpayUser) service.loadById(MpayUser.class, m.getId());
		if (mpay.getMpayPassword().equals(oldpass)) {
			mpay.setMpayPassword(newpass);
			service.saveOrUpdate(mpay);
			message = "密码修改成功！";
			success = true;
		} else {
			message = "原密码错误！";
		}
		return SUCCESS;
	}

	public String savewap() {
		String userid = this.user;
		String pwd = this.pass;
		String path = this.getServletContext().getRealPath("/");
		if (path != null)
			path = path.replaceAll("\\\\", "/");
		else
			path = "/";
		if (!path.endsWith("/"))
			path = path + "/";
		Util.ROOTPATH = path;
		LOG.info("wap " + userid + "_正在登陆...");
		MpayUser m = (MpayUser) service
				.loadObject(" from MpayUser where (mpay_userid = '" + userid
						+ "' or mpay_termid = '" + userid
						+ "') and mpay_password = '" + pwd
						+ "' and mpay_flag = '0'");
		if (m != null) {
			QQWry w = new QQWry();
			w.setPath(Util.ROOTPATH + "/QQWry.Dat");
			try {
				w.seek(getRequest().getRemoteAddr() + "");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.getRequest().setAttribute("userName", m.getMpayUsername());
			this.getRequest().setAttribute("lastLogin", m.getMpayLogtime());
			m.setMpayLogip(getRequest().getRemoteAddr() + "[" + w.getCountry()
					+ "_" + w.getLocal() + "]");
			m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
			m.setMpayLogCount(m.getMpayLogCount() + 1);
			service.saveOrUpdate(m);
			UserView v = (UserView) service
					.loadObject(" from UserView where mpay_userid = '" + userid
							+ "' and mpay_password = '" + pwd
							+ "' and mpay_flag = '0'");
			getRequest().getSession().setAttribute("mpay_user", v);
			System.out.println("登陆成功");
			return SUCCESS;
		} else {
			System.out.println("登陆失败");
			this.getRequest().setAttribute("action", "loginFailure");
			this.getRequest().setAttribute("message", "用户不存在或密码错误！");
			return ERROR;
		}
	}

	public String save() {
		String userid = this.user;
		String pwd = this.pass;
		String code = this.checkcode;
		LOG.info(userid + "_正在登陆..." + "pas:" + pwd);
		String path = this.getServletContext().getRealPath("/");
		if (path != null)
			path = path.replaceAll("\\\\", "/");
		else
			path = "/";
		if (!path.endsWith("/"))
			path = path + "/";
		Util.ROOTPATH = path;
		List readylist;
		String check = (String) getRequest().getSession().getAttribute("rand");
		if (code.equals(check)) {
			MpayUser m = (MpayUser) service
					.loadObject(" from MpayUser where (mpay_userid = '"
							+ userid + "' or mpay_termid = '" + userid
							+ "' )and mpay_password = '" + pwd
							+ "' and mpay_flag = '0'");
			if (m != null) {
				this.success = true;
				QQWry w = new QQWry();
				w.setPath(Util.ROOTPATH + "/QQWry.Dat");
//				try {
//					PaymentEnvironment.initialize(Util.ROOTPATH + "/webPay/");
//				} catch (Exception e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					w.seek(getRequest().getRemoteAddr() + "");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				m.setMpayLogip(getRequest().getRemoteAddr() + "["
						+ w.getCountry() + "_" + w.getLocal() + "]");
				m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				m.setMpayLogCount(m.getMpayLogCount() + 1);
				List tmp = service.query(" from MpayOrg where shop_id = '"
						+ m.getShopId() + "'");
				String termorg = "";
				for (int i = 0; i < tmp.size(); i++) {
					MpayOrg mm = (MpayOrg) tmp.get(i);
					termorg += "'" + mm.getTermid().trim() + "',";
				}
				if(termorg.indexOf(",")!=-1){
				termorg = termorg.substring(0,termorg.lastIndexOf(","));
				}
				getRequest().getSession().setAttribute("termgroup", termorg);
				service.saveOrUpdate(m);
				UserView v = (UserView) service
						.loadObject(" from UserView where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' ) and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				getRequest().getSession().setAttribute("mpay_user", v);
				this.message = "登陆成功!";
				// 获取用户对应的系统应用菜单
				MpayGroup group = (MpayGroup) service.loadById(MpayGroup.class,
						Integer.valueOf(m.getMpayGroupid()));
				// System.out.println(group.getMpayModuleId());
				List allModuleList;
				if (userid.equals("admin")
						&& getRequest().getRemoteAddr().equals("127.0.0.1")) {
					allModuleList = service
							.query(" from MpayModule order by mpay_order");
				} else {
					allModuleList = service
							.query(" from MpayModule where id in ("
									+ group.getMpayModuleId()
									+ ") order by mpay_order");
				}
				Iterator menu = allModuleList.iterator();
				Vector parentV = new Vector();
				Vector childV = new Vector();
				while (menu.hasNext()) {
					MpayModule mm = (MpayModule) menu.next();
					if (mm.getMpayFatherid().equals("0")) {
						parentV.add(mm);
					} else {
						childV.add(mm);
					}
				}
				getRequest().getSession().setAttribute("parentVector", parentV);
				getRequest().getSession().setAttribute("childVector", childV);
				// getRequest().getSession().setAttribute("rand",null);
				// code="";
			} else if ((readylist = serviceOracle
					.query("from CsSysTerminalBak where concat(areano,terminalno) = '"
							+ userid
							+ "' and substr(bangdingaccounts,length(bangdingaccounts)-5) = '"
							+ pwd + "' and state = '1'")).size() > 0) {
				System.out.println(userid + "_新用户注册...");
				CsSysTerminalBak cs = (CsSysTerminalBak) readylist.get(0);
				TermReadyView ready = new TermReadyView();
				ready.setAddress(cs.getInstalladdress());
				ready.setAppdt(cs.getAppdt());
				ready.setBank(cs.getGroupid());
				ready.setIncdno(cs.getBangdingaccounts());
				ready.setLinkman(cs.getLinkman());
				ready.setLinkmanphone(cs.getLinkmanphone());
				ready.setTermid(cs.getAreano().trim()
						+ cs.getTerminalno().trim());
				ready.setUpdt(cs.getUpdatedt());
				ready.setUsername(cs.getUsername());
				try {
					service.saveUser(ready);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				m = (MpayUser) service
						.loadObject(" from MpayUser where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' )and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				this.success = true;
				QQWry w = new QQWry();
				w.setPath(Util.ROOTPATH + "/QQWry.Dat");
//				try {
//					PaymentEnvironment.initialize(Util.ROOTPATH + "/webPay/");
//				} catch (Exception e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					w.seek(getRequest().getRemoteAddr() + "");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				m.setMpayLogip(getRequest().getRemoteAddr() + "["
						+ w.getCountry() + "_" + w.getLocal() + "]");
				m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				m.setMpayLogCount(m.getMpayLogCount() + 1);
				List tmp = service.query(" from MpayOrg where shop_id = '"
						+ m.getShopId() + "'");
				String termorg = "";
				for (int i = 0; i < tmp.size(); i++) {
					MpayOrg mm = (MpayOrg) tmp.get(i);
					termorg += "'" + mm.getTermid() + "',";
				}
				termorg += "''";
				getRequest().getSession().setAttribute("termgroup", termorg);
				try {
					service.saveOrUpdate(m);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				UserView v = (UserView) service
						.loadObject(" from UserView where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' ) and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				getRequest().getSession().setAttribute("mpay_user", v);
				this.message = "登陆成功!";
				// 获取用户对应的系统应用菜单
				MpayGroup group = (MpayGroup) service.loadById(MpayGroup.class,
						Integer.valueOf(m.getMpayGroupid()));
				System.out.println(group.getMpayModuleId());
				List allModuleList = service
						.query(" from MpayModule where id in ("
								+ group.getMpayModuleId()
								+ ") order by mpay_order");
				Iterator menu = allModuleList.iterator();
				Vector parentV = new Vector();
				Vector childV = new Vector();
				while (menu.hasNext()) {
					MpayModule mm = (MpayModule) menu.next();
					if (mm.getMpayFatherid().equals("0")) {
						parentV.add(mm);
					} else {
						childV.add(mm);
					}
				}
				getRequest().getSession().setAttribute("parentVector", parentV);
				getRequest().getSession().setAttribute("childVector", childV);
			} else if ((readylist = serviceOracle
					.query("from CsSysTerminalHb where concat(areano,terminalno) = '"
							+ userid
							+ "' and substr(bangdingaccounts,length(bangdingaccounts)-5) = '"
							+ pwd + "' and state = '1'")).size() > 0) {
				System.out.println(userid + "_新用户注册HB...");
				CsSysTerminalHb cs = (CsSysTerminalHb) readylist.get(0);
				TermReadyView ready = new TermReadyView();
				ready.setAddress(cs.getInstalladdress());
				ready.setAppdt(cs.getAppdt());
				ready.setBank(cs.getGroupid());
				ready.setIncdno(cs.getBangdingaccounts());
				ready.setLinkman(cs.getLinkman());
				ready.setLinkmanphone(cs.getLinkmanphone());
				ready.setTermid(cs.getAreano().trim()
						+ cs.getTerminalno().trim());
				ready.setUpdt(cs.getUpdatedt());
				ready.setUsername(cs.getUsername());
				try {
					service.saveUser(ready);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				m = (MpayUser) service
						.loadObject(" from MpayUser where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' )and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				this.success = true;
				QQWry w = new QQWry();
				w.setPath(Util.ROOTPATH + "/QQWry.Dat");
//				try {
//					PaymentEnvironment.initialize(Util.ROOTPATH + "/webPay/");
//				} catch (Exception e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					w.seek(getRequest().getRemoteAddr() + "");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				m.setMpayLogip(getRequest().getRemoteAddr() + "["
						+ w.getCountry() + "_" + w.getLocal() + "]");
				m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				m.setMpayLogCount(m.getMpayLogCount() + 1);
				List tmp = service.query(" from MpayOrg where shop_id = '"
						+ m.getShopId() + "'");
				String termorg = "";
				for (int i = 0; i < tmp.size(); i++) {
					MpayOrg mm = (MpayOrg) tmp.get(i);
					termorg += "'" + mm.getTermid() + "',";
				}
				termorg += "''";
				getRequest().getSession().setAttribute("termgroup", termorg);
				try {
					service.saveOrUpdate(m);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				UserView v = (UserView) service
						.loadObject(" from UserView where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' ) and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				getRequest().getSession().setAttribute("mpay_user", v);
				this.message = "登陆成功!";
				// 获取用户对应的系统应用菜单
				MpayGroup group = (MpayGroup) service.loadById(MpayGroup.class,
						Integer.valueOf(m.getMpayGroupid()));
				System.out.println(group.getMpayModuleId());
				List allModuleList = service
						.query(" from MpayModule where id in ("
								+ group.getMpayModuleId()
								+ ") order by mpay_order");
				Iterator menu = allModuleList.iterator();
				Vector parentV = new Vector();
				Vector childV = new Vector();
				while (menu.hasNext()) {
					MpayModule mm = (MpayModule) menu.next();
					if (mm.getMpayFatherid().equals("0")) {
						parentV.add(mm);
					} else {
						childV.add(mm);
					}
				}
				getRequest().getSession().setAttribute("parentVector", parentV);
				getRequest().getSession().setAttribute("childVector", childV);
			} else if ((readylist = serviceOracle
					.query("from CsSysTerminal where concat(areano,terminalno) = '"
							+ userid
							+ "' and substr(bangdingaccounts,length(bangdingaccounts)-5) = '"
							+ pwd + "' and state = '1'")).size() > 0) {
				System.out.println(userid + "_新用户注册HS...");
				CsSysTerminal cs = (CsSysTerminal) readylist.get(0);
				TermReadyView ready = new TermReadyView();
				ready.setAddress(cs.getInstalladdress());
				ready.setAppdt(cs.getAppdt());
				ready.setBank(cs.getGroupid());
				ready.setIncdno(cs.getBangdingaccounts());
				ready.setLinkman(cs.getLinkman());
				ready.setLinkmanphone(cs.getLinkmanphone());
				ready.setTermid(cs.getAreano().trim()
						+ cs.getTerminalno().trim());
				ready.setUpdt(cs.getUpdatedt());
				ready.setUsername(cs.getUsername());
				ready.setTable("3");
				try {
					service.saveUser(ready);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				m = (MpayUser) service
						.loadObject(" from MpayUser where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' )and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				this.success = true;
				QQWry w = new QQWry();
				w.setPath(Util.ROOTPATH + "/QQWry.Dat");
//				try {
//					PaymentEnvironment.initialize(Util.ROOTPATH + "/webPay/");
//				} catch (Exception e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					w.seek(getRequest().getRemoteAddr() + "");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				m.setMpayLogip(getRequest().getRemoteAddr() + "["
						+ w.getCountry() + "_" + w.getLocal() + "]");
				m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				m.setMpayLogCount(m.getMpayLogCount() + 1);
				List tmp = service.query(" from MpayOrg where shop_id = '"
						+ m.getShopId() + "'");
				String termorg = "";
				for (int i = 0; i < tmp.size(); i++) {
					MpayOrg mm = (MpayOrg) tmp.get(i);
					termorg += "'" + mm.getTermid() + "',";
				}
				termorg += "''";
				getRequest().getSession().setAttribute("termgroup", termorg);
				try {
					service.saveOrUpdate(m);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				UserView v = (UserView) service
						.loadObject(" from UserView where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' ) and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				getRequest().getSession().setAttribute("mpay_user", v);
				this.message = "登陆成功!";
				// 获取用户对应的系统应用菜单
				MpayGroup group = (MpayGroup) service.loadById(MpayGroup.class,
						Integer.valueOf(m.getMpayGroupid()));
				System.out.println(group.getMpayModuleId());
				List allModuleList = service
						.query(" from MpayModule where id in ("
								+ group.getMpayModuleId()
								+ ") order by mpay_order");
				Iterator menu = allModuleList.iterator();
				Vector parentV = new Vector();
				Vector childV = new Vector();
				while (menu.hasNext()) {
					MpayModule mm = (MpayModule) menu.next();
					if (mm.getMpayFatherid().equals("0")) {
						parentV.add(mm);
					} else {
						childV.add(mm);
					}
				}
				getRequest().getSession().setAttribute("parentVector", parentV);
				getRequest().getSession().setAttribute("childVector", childV);
			} else if ((readylist = serviceOracle
					.query("from CsSysTerminal where concat('POS',terminalno) = '"
							+ userid.toUpperCase() + "'  and state = '1'"))
					.size() > 0) {
				System.out.println(userid + "_新用户注册POS...");
				CsSysTerminal cs = (CsSysTerminal) readylist.get(0);
				TermReadyView ready = new TermReadyView();
				ready.setAddress(cs.getInstalladdress());
				ready.setAppdt(cs.getAppdt());
				ready.setBank(cs.getGroupid());
				ready.setIncdno(pwd);
				ready.setLinkman(cs.getLinkman());
				ready.setLinkmanphone(cs.getLinkmanphone());
				ready.setTermid("POS" + cs.getTerminalno().trim());
				ready.setUpdt(cs.getUpdatedt());
				ready.setUsername(cs.getUsername());
				ready.setTable("3");
				try {
					service.saveUser(ready);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					this.success = false;
					this.message = "用户名或密码错误!";
					getRequest().setAttribute("msg", message);
					LOG.info(message);
					if (success)
						return SUCCESS;
					else
						return ERROR;
				}

				m = (MpayUser) service
						.loadObject(" from MpayUser where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' )and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				this.success = true;
				QQWry w = new QQWry();
				w.setPath(Util.ROOTPATH + "/QQWry.Dat");
//				try {
//					PaymentEnvironment.initialize(Util.ROOTPATH + "/webPay/");
//				} catch (Exception e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					w.seek(getRequest().getRemoteAddr() + "");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				m.setMpayLogip(getRequest().getRemoteAddr() + "["
						+ w.getCountry() + "_" + w.getLocal() + "]");
				m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				m.setMpayLogCount(m.getMpayLogCount() + 1);
				List tmp = service.query(" from MpayOrg where shop_id = '"
						+ m.getShopId() + "'");
				String termorg = "";
				for (int i = 0; i < tmp.size(); i++) {
					MpayOrg mm = (MpayOrg) tmp.get(i);
					termorg += "'" + mm.getTermid() + "',";
				}
				termorg += "''";
				getRequest().getSession().setAttribute("termgroup", termorg);
				try {
					service.saveOrUpdate(m);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				UserView v = (UserView) service
						.loadObject(" from UserView where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' ) and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				getRequest().getSession().setAttribute("mpay_user", v);
				this.message = "登陆成功!";
				// 获取用户对应的系统应用菜单
				MpayGroup group = (MpayGroup) service.loadById(MpayGroup.class,
						Integer.valueOf(m.getMpayGroupid()));
				System.out.println(group.getMpayModuleId());
				List allModuleList = service
						.query(" from MpayModule where id in ("
								+ group.getMpayModuleId()
								+ ") order by mpay_order");
				Iterator menu = allModuleList.iterator();
				Vector parentV = new Vector();
				Vector childV = new Vector();
				while (menu.hasNext()) {
					MpayModule mm = (MpayModule) menu.next();
					if (mm.getMpayFatherid().equals("0")) {
						parentV.add(mm);
					} else {
						childV.add(mm);
					}
				}
				getRequest().getSession().setAttribute("parentVector", parentV);
				getRequest().getSession().setAttribute("childVector", childV);
			} else if ((readylist = serviceOracle
					.query("from CsSysTerminal where concat(areano,terminalno) = '"
							+ userid.toUpperCase() + "'  and state = '1'"))
					.size() > 0) {
				System.out.println(userid + "_新用户注册HS_111...");
				CsSysTerminal cs = (CsSysTerminal) readylist.get(0);
				TermReadyView ready = new TermReadyView();
				ready.setAddress(cs.getInstalladdress());
				ready.setAppdt(cs.getAppdt());
				ready.setBank(cs.getGroupid());
				ready.setIncdno(pwd);
				ready.setLinkman(cs.getLinkman());
				ready.setLinkmanphone(cs.getLinkmanphone());
				ready.setTermid(cs.getAreano().trim()
						+ cs.getTerminalno().trim());
				ready.setUpdt(cs.getUpdatedt());
				ready.setUsername(cs.getUsername());
				ready.setTable("3");
				try {
					service.saveUser(ready);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					this.success = false;
					this.message = "用户名或密码错误!";
					getRequest().setAttribute("msg", message);
					LOG.info(message);
					if (success)
						return SUCCESS;
					else
						return ERROR;
				}

				m = (MpayUser) service
						.loadObject(" from MpayUser where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' )and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				this.success = true;
				QQWry w = new QQWry();
				w.setPath(Util.ROOTPATH + "/QQWry.Dat");
//				try {
//					PaymentEnvironment.initialize(Util.ROOTPATH + "/webPay/");
//				} catch (Exception e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					w.seek(getRequest().getRemoteAddr() + "");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				m.setMpayLogip(getRequest().getRemoteAddr() + "["
						+ w.getCountry() + "_" + w.getLocal() + "]");
				m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				m.setMpayLogCount(m.getMpayLogCount() + 1);
				List tmp = service.query(" from MpayOrg where shop_id = '"
						+ m.getShopId() + "'");
				String termorg = "";
				for (int i = 0; i < tmp.size(); i++) {
					MpayOrg mm = (MpayOrg) tmp.get(i);
					termorg += "'" + mm.getTermid() + "',";
				}
				termorg += "''";
				getRequest().getSession().setAttribute("termgroup", termorg);
				try {
					service.saveOrUpdate(m);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				UserView v = (UserView) service
						.loadObject(" from UserView where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' ) and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				getRequest().getSession().setAttribute("mpay_user", v);
				this.message = "登陆成功!";
				// 获取用户对应的系统应用菜单
				MpayGroup group = (MpayGroup) service.loadById(MpayGroup.class,
						Integer.valueOf(m.getMpayGroupid()));
				System.out.println(group.getMpayModuleId());
				List allModuleList = service
						.query(" from MpayModule where id in ("
								+ group.getMpayModuleId()
								+ ") order by mpay_order");
				Iterator menu = allModuleList.iterator();
				Vector parentV = new Vector();
				Vector childV = new Vector();
				while (menu.hasNext()) {
					MpayModule mm = (MpayModule) menu.next();
					if (mm.getMpayFatherid().equals("0")) {
						parentV.add(mm);
					} else {
						childV.add(mm);
					}
				}
				getRequest().getSession().setAttribute("parentVector", parentV);
				getRequest().getSession().setAttribute("childVector", childV);
			} else if ((readylist = serviceOracle
					.query("from MklInfo where MKL_ID = '"
							+ userid + "'  and substr(MKL_NO,length(MKL_NO)-5) = '"
							+ pwd + "' ")).size() > 0){
				System.out.println(userid + "_新用户注册行业卡...");
				MklInfo cs = (MklInfo) readylist.get(0);
				TermReadyView ready = new TermReadyView();
				ready.setAddress(cs.getMklAdd());
				ready.setAppdt(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				ready.setBank(cs.getMklPro());
				ready.setIncdno(pwd);
				ready.setLinkman(cs.getMklLinkman());
				ready.setLinkmanphone("-");
				ready.setTermid("C"+cs.getMklId());
				ready.setUpdt("-");
				ready.setUsername(cs.getMklCom());
				ready.setTable("3");
				try {
					service.saveUser(ready);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					this.success = false;
					this.message = "用户名或密码错误!";
					getRequest().setAttribute("msg", message);
					LOG.info(message);
					if (success)
						return SUCCESS;
					else
						return ERROR;
				}

				m = (MpayUser) service
						.loadObject(" from MpayUser where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' )and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				this.success = true;
				QQWry w = new QQWry();
				w.setPath(Util.ROOTPATH + "/QQWry.Dat");
//				try {
//					PaymentEnvironment.initialize(Util.ROOTPATH + "/webPay/");
//				} catch (Exception e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					w.seek(getRequest().getRemoteAddr() + "");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				m.setMpayLogip(getRequest().getRemoteAddr() + "["
						+ w.getCountry() + "_" + w.getLocal() + "]");
				m.setMpayLogtime(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				m.setMpayLogCount(m.getMpayLogCount() + 1);
				List tmp = service.query(" from MpayOrg where shop_id = '"
						+ m.getShopId() + "'");
				String termorg = "";
				for (int i = 0; i < tmp.size(); i++) {
					MpayOrg mm = (MpayOrg) tmp.get(i);
					termorg += "'" + mm.getTermid() + "',";
				}
				termorg += "''";
				getRequest().getSession().setAttribute("termgroup", termorg);
				try {
					service.saveOrUpdate(m);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				UserView v = (UserView) service
						.loadObject(" from UserView where (mpay_userid = '"
								+ userid + "' or mpay_termid = '" + userid
								+ "' ) and mpay_password = '" + pwd
								+ "' and mpay_flag = '0'");
				getRequest().getSession().setAttribute("mpay_user", v);
				this.message = "登陆成功!";
				// 获取用户对应的系统应用菜单
				MpayGroup group = (MpayGroup) service.loadById(MpayGroup.class,
						Integer.valueOf(m.getMpayGroupid()));
				System.out.println(group.getMpayModuleId());
				List allModuleList = service
						.query(" from MpayModule where id in ("
								+ group.getMpayModuleId()
								+ ") order by mpay_order");
				Iterator menu = allModuleList.iterator();
				Vector parentV = new Vector();
				Vector childV = new Vector();
				while (menu.hasNext()) {
					MpayModule mm = (MpayModule) menu.next();
					if (mm.getMpayFatherid().equals("0")) {
						parentV.add(mm);
					} else {
						childV.add(mm);
					}
				}
				getRequest().getSession().setAttribute("parentVector", parentV);
				getRequest().getSession().setAttribute("childVector", childV);
			}else {
				this.success = false;
				this.message = "用户名或密码错误!";
			}
		} else if (getRequest().getSession().getAttribute("rand") == null) {
			this.success = false;
			this.message = "验证码失效，请刷新页面!";
		} else {
			this.success = false;
			this.message = "验证码输入错误!";
		}
		
		getRequest().setAttribute("msg", message);
		LOG.info(message);
		if (success)
			return SUCCESS;
		else
			return ERROR;
	}

	public String getMenu() {
		LOG.info("获取菜单信息...");
		UserView m = (UserView) getRequest().getSession().getAttribute(
				"mpay_user");
		MpayGroup mg = (MpayGroup) service
				.loadObject("from MpayGroup where id = '" + m.getMpayGroupid()
						+ "'");
		// System.out.println(mg.getMpayModuleId());
		List moduleList = service.query("from MpayModule where id in ("
				+ mg.getMpayModuleId() + ") order by mpay_order");
		// List moduleList = service.listAll("MpayModule");
		Vector parentV = new Vector();
		Vector childV = new Vector();
		Iterator menu = moduleList.iterator();
		while (menu.hasNext()) {
			MpayModule mm = (MpayModule) menu.next();
			// System.out.println(" ModeuleName = "+m.getModuleName());
			if (mm.getMpayFatherid().equals("0")) {
				parentV.add(mm);
			} else {
				childV.add(mm);
			}
		}
		menus = new ArrayList<Menu>();
		for (int i = 0; i < parentV.size(); i++) {
			MpayModule mp = (MpayModule) parentV.get(i);
			Menu menup = new Menu();
			List<Menu> List = new ArrayList<Menu>();
			menup.setText(mp.getMpayText());
			menup.setId(mp.getMpayModuleKey());
			menup.setLeaf(false);
			menup.setIcon(mp.getMpayIcon());
			// menup.setCls("folder");
			menus.add(menup);
			for (int j = 0; j < childV.size(); j++) {
				MpayModule mc = (MpayModule) childV.get(j);
				if (Integer.valueOf(mc.getMpayFatherid()) == mp.getId()
						.intValue()) {
					Menu menuc = new Menu();
					menuc.setId(mc.getMpayModuleKey());
					menuc.setIcon(mc.getMpayIcon());
					// menuc.setCls("file");
					menuc.setLeaf(true);
					if (mc.getMpayRemark() != null)
						menuc.setQtip(mc.getMpayRemark());
					menuc.setText(mc.getMpayText());
					List.add(menuc);
				}
			}
			menup.setChildren(List);
		}
		return SUCCESS;
	}

	public String getUserInfo() {
		LOG.info("读取用户列表数据...");
		int s = Integer.valueOf(start);
		int l = Integer.valueOf(limit);
		String sql = " where '1' = '1' ";
		userid = userid == null ? "" : userid.trim();
		username = username == null ? "" : username.trim();
		if (!userid.equals("")) {
			sql += " and mpay_userid like '%" + userid + "%' ";
		}
		if (!username.equals("")) {
			sql += " and mpay_username like '%" + username + "%' ";
		}
		int start = 1;
		if (s != 0)
			start = s / 30 + 1;
		// System.out.println(sql);
		List userList = service.query("from UserView" + sql, start, l);
		page = new Page();
		page.setRoot(userList);
		page.setTotalProperty(service
				.countQuery("select count(*) from UserView" + sql));
		page.setStart(++s);
		page.setLimit(l = l == 0 ? 30 : l);
		return SUCCESS;
	}

	public String changeUser() {
		LOG.info("改变用户状态...");
		MpayUser m = (MpayUser) service.loadById(MpayUser.class, Integer
				.valueOf(id));
		if (type == null || type.equals("")) {
			if (m.getMpayFlag().equals("0")) {
				m.setMpayFlag("1");
			} else {
				m.setMpayFlag("0");
			}
			message = "变更状态成功!";
		}else{
			m.setMpayPassword("111111");
			message = "密码重置成功!（新密码:111111）";
		}
		service.saveOrUpdate(m);
		
		return SUCCESS;
	}

	public String deleteterm() {
		LOG.info("删除绑定终端...");
		service.delById(MpayOrg.class, Integer.valueOf(id));
		message = "删除绑定终端成功!";
		return SUCCESS;
	}

	public String getOrg() {
		LOG.info("获得绑定终端列表...");
		page = new Page();
		orglist = service.query("from MpayOrg where shop_id = '" + id + "'");
		page.setRoot(orglist);
		return SUCCESS;
	}

	public String getGroup() {
		LOG.info("获得用户组列表");
		grouplist = service.listAll("MpayGroup");
		return SUCCESS;
	}

	public String getTable() {
		LOG.info("获得所属平台列表");
		tablelist = service.listAll("MpayTradeTable");
		return SUCCESS;
	}

	public String getShop() {
		LOG.info("获得商户列表");
		// List tmp = service.listAll("MpayShop");
		// shoplist = new ArrayList<MpayShop>();
		// for(int i = 0;i<tmp.size();i++){
		// MpayShop m = (MpayShop)tmp.get(i);
		// m.setShopName(m.getShopCode()+"|"+m.getShopName());
		// shoplist.add(m);
		// }
		shoplist = service.listAll("MpayShop");
		return SUCCESS;
	}

	public String addGroup() {
		LOG.info("新增绑定终端...");
		MpayOrg m = new MpayOrg();
		m.setTermCardno(cardno == null || cardno.equals("") ? "-" : cardno);
		System.out.println(termid);
		m.setTermid(termid);
		m.setShopId(id);
		m.setUserid("-");
		m.setTermName(termname);
		m.setTermLinkphone(linkphone == null || linkphone.equals("") ? "-"
				: linkphone);
		m
				.setTermLinkname(linkman == null || linkman.equals("") ? "-"
						: linkman);
		m.setTermLinkaddress(address == null || address.equals("") ? "-"
				: address);
		m.setTermNick(nick == null || nick.equals("") ? "-" : nick);
		m.setTermPsam(psam == null || psam.equals("") ? "-" : psam);
		m.setTermCreateDt(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		m.setTermUpdateDt("-");
		try {
			service.saveOrUpdate(m);
		} catch (Exception e) {
			e.printStackTrace();
			message = "绑定终端失败，请确认需绑定终端号是否已绑定!";
			success = false;

			return SUCCESS;
		}
		message = "绑定终端成功!";
		success = true;
		return SUCCESS;
	}

	public String addUser() {
		LOG.info("新增用户。。。");
		MpayUser m = new MpayUser();
		m.setMpayCreate(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		m.setMpayFlag("0");
		m.setMpayLogCount(0);
		m.setMpayLogip("-");
		m.setMpayLogip("-");
		m.setMpayLogtime("-");
		m.setMpayTermid(termid);
		m.setMpayPassword("111111");
		m.setMpayUserid(userid);
		m.setMpayUsername(username);

		m.setIsAll(isall.equals("否") ? 0 : 1);
		List tmp = service.query("from MpayGroup where mpay_module_name = '"
				+ groupid + "'");
		if (tmp.size() == 0) {
			message = "读取用户组参数异常！";
			success = false;
			return SUCCESS;
		}
		m.setMpayGroupid(((MpayGroup) tmp.get(0)).getId() + "");
		tmp = service.query("from MpayShop where shop_name = '" + shopid + "'");
		if (tmp.size() == 0) {
			message = "读取商户参数异常！";
			success = false;
			return SUCCESS;
		}
		m.setShopId(((MpayShop) tmp.get(0)).getId() + "");
		tmp = service.query("from MpayTradeTable where system_name = '"
				+ isadmin + "'");
		if (tmp.size() == 0) {
			message = "读取平台参数异常！";
			success = false;
			return SUCCESS;
		}
		m.setIsAdmin(((MpayTradeTable) tmp.get(0)).getId());
		try {
			service.saveOrUpdate(m);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message = "新增用户有误，请确认登录名是否重复！";
			success = false;
			return SUCCESS;
		}
		message = "新增用户成功！";
		success = true;
		return SUCCESS;
	}

	public String updateUser() {
		LOG.info("保存更新数据...");
		// System.out.println(data);
		List tmp = service.listAll("MpayGroup");
		HashMap map = new HashMap();
		for (int i = 0; i < tmp.size(); i++) {
			MpayGroup m = (MpayGroup) tmp.get(i);
			map.put(m.getMpayModuleName(), m.getId());
		}
		String[] obj = data.split("\\|");
		for (int i = 0; i < obj.length; i++) {
			String[] s = obj[i].split(",");
			MpayUser m = (MpayUser) service.loadById(MpayUser.class, Integer
					.valueOf(s[0]));
			m.setMpayUserid(s[1]);
			m.setMpayUsername(s[2]);
			m.setMpayTermid(s[3]);
			m.setMpayGroupid(map.get(s[4]) + "");
			m.setIsAdmin(Integer.valueOf(s[5]));
			m.setIsAll(Integer.valueOf(s[6]));
			m.setMpayFlag(s[7]);
			service.saveOrUpdate(m);
		}
		// System.out.println(data);
		message = "更新成功!";
		success = true;
		return SUCCESS;
	}

	AdminService service;
	AdminService serviceOracle;
	private boolean success;
	private String message;
	private String user;
	private String userid;
	private String username;
	private String pass;
	private String checkcode;
	private List<Menu> menus;
	private List<MpayOrg> orglist;
	private List<MpayGroup> grouplist;
	private List<MpayShop> shoplist;
	private List<MpayTradeTable> tablelist;
	private String data;
	private String node;
	private Page page;
	private String start = "0";
	private String limit = "30";
	private String id;
	private String termid;
	private String groupid;
	private String shopid;
	private String isadmin;
	private String isall;
	private String shopname;
	private String linkman;
	private String cardno;
	private String linkphone;
	private String address;
	private String nick;
	private String oldpass;
	private String newpass;
	private String newpass2;
	private String shopcode;
	private String psam;
	private String termname;
	private String termcode;
	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public AdminService getServiceOracle() {
		return serviceOracle;
	}

	public void setServiceOracle(AdminService serviceOracle) {
		this.serviceOracle = serviceOracle;
	}

	public List<MpayTradeTable> getTablelist() {
		return tablelist;
	}

	public void setTablelist(List<MpayTradeTable> tablelist) {
		this.tablelist = tablelist;
	}

	public String getTermname() {
		return termname;
	}

	public void setTermname(String termname) {
		this.termname = termname;
	}

	public String getTermcode() {
		return termcode;
	}

	public void setTermcode(String termcode) {
		this.termcode = termcode;
	}

	public String getShopcode() {
		return shopcode;
	}

	public void setShopcode(String shopcode) {
		this.shopcode = shopcode;
	}

	public String getPsam() {
		return psam;
	}

	public void setPsam(String psam) {
		this.psam = psam;
	}

	public List<MpayShop> getShoplist() {
		return shoplist;
	}

	public void setShoplist(List<MpayShop> shoplist) {
		this.shoplist = shoplist;
	}

	public String getOldpass() {
		return oldpass;
	}

	public void setOldpass(String oldpass) {
		this.oldpass = oldpass;
	}

	public String getNewpass() {
		return newpass;
	}

	public void setNewpass(String newpass) {
		this.newpass = newpass;
	}

	public String getNewpass2() {
		return newpass2;
	}

	public String getShopid() {
		return shopid;
	}

	public void setShopid(String shopid) {
		this.shopid = shopid;
	}

	public void setNewpass2(String newpass2) {
		this.newpass2 = newpass2;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	public String getShopname() {
		return shopname;
	}

	public void setShopname(String shopname) {
		this.shopname = shopname;
	}

	public String getLinkman() {
		return linkman;
	}

	public void setLinkman(String linkman) {
		this.linkman = linkman;
	}

	public String getCardno() {
		return cardno;
	}

	public void setCardno(String cardno) {
		this.cardno = cardno;
	}

	public String getLinkphone() {
		return linkphone;
	}

	public void setLinkphone(String linkphone) {
		this.linkphone = linkphone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTermid() {
		return termid;
	}

	public void setTermid(String termid) {
		this.termid = termid;
	}

	public String getGroupid() {
		return groupid;
	}

	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}

	public String getIsadmin() {
		return isadmin;
	}

	public void setIsadmin(String isadmin) {
		this.isadmin = isadmin;
	}

	public String getIsall() {
		return isall;
	}

	public void setIsall(String isall) {
		this.isall = isall;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public List<Menu> getMenus() {
		return menus;
	}

	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getCheckcode() {
		return checkcode;
	}

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public AdminService getService() {
		return service;
	}

	public void setService(AdminService service) {
		this.service = service;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public List<MpayGroup> getGrouplist() {
		return grouplist;
	}

	public void setGrouplist(List<MpayGroup> grouplist) {
		this.grouplist = grouplist;
	}

	public List<MpayOrg> getOrglist() {
		return orglist;
	}

	public void setOrglist(List<MpayOrg> orglist) {
		this.orglist = orglist;
	}
}
